package main

import "fmt"

type treeNode struct {
	value       int
	left, right *treeNode
}

func (node treeNode) print() {
	fmt.Print(node.value, " ")
}

/*
这里不传指针类型，传进来的是值，并非引用，因此修改不生效
*/
func (node *treeNode) setValue(value int) {
	if node == nil {
		fmt.Println("指针不能为nil")
		return
	}
	node.value = value
}

func (node *treeNode) preOrderTraverse() {
	if node != nil {
		node.print()
		node.left.preOrderTraverse()
		node.right.preOrderTraverse()
	}
}

func createNode(value int) *treeNode {
	return &treeNode{value: value}
}

func main() {
	var root treeNode = treeNode{value: 3}
	root.left = &treeNode{}
	root.right = &treeNode{5, nil, nil}
	root.right.left = new(treeNode)
	root.left.right = createNode(6)
	fmt.Println(root)
	fmt.Println(root.right)
	fmt.Println(root.right.left)

	/*nodes := []treeNode{
		{value: 3},
		{},
		{5, nil, &root},
	}
	fmt.Println(nodes)*/

	/*root.print()
	root.right.left.setValue(4)
	root.right.left.print()*/
	/*var pRoot *treeNode
	// nil的指针调用的时候会出错
	pRoot.setValue(200)
	pRoot = &root
	pRoot.setValue(300)
	pRoot.print()

	qRoot := root
	qRoot.setValue(500)
	qRoot.print()*/

	root.preOrderTraverse()
}
